package com.social.service.impl;


import com.social.dao.EsActivityRepository;
import com.social.pojo.Activity;
import com.social.pojo.Result;
import com.social.pojo.es.EsActivity;
import com.social.service.EsActivityService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.Optional;

/***
 * @author wjl
 * @date 2023/1/7
 * @desc
 **/
@Service
public class EsActivityServiceImpl implements EsActivityService {

    @Autowired
    private EsActivityRepository esActivityRepository;


    //es添加数据
    public void addToES(Activity activity) {
        EsActivity EsActivity = new EsActivity();
        BeanUtils.copyProperties(activity,EsActivity);
        esActivityRepository.save(EsActivity);
    }

    //es修改数据
    public Result esUpdate(EsActivity EsActivity){
        // 注意，es是根据id修改信息的，id必须是es里面的id,而非mysql数据库里面的id
        EsActivity activity= searchById(EsActivity.getId());
        if (activity!=null){
            EsActivity.setBegintime(activity.getBegintime());
            EsActivity.setEndtime(new Date());
            esActivityRepository.save(EsActivity);
            return Result.ok(200);
        }
        return Result.fail(201,"更新失败");
    }
    public void esDelete(EsActivity activity){
        // 注意，es是根据id删除信息的，id必须是es里面的id,而非mysql数据库里面的id
        esActivityRepository.deleteById(activity.getId());
    }
    //根据title或desc查询
    public EsActivity searchById(String id) {
        Optional<EsActivity> EsActivity = esActivityRepository.findById(id);
        return EsActivity.get();
    }

    //根据title或desc查询
    public Result searchactivity(String keyword) {
        return Result.ok(200,esActivityRepository.findByTitleOrContent(keyword, keyword));
    }
    //根据title查询并高亮
    public Result searchTitleWithHits(String keyword) {
        return Result.ok(200,esActivityRepository.findTitle(keyword));
    }
    //根据title或desc查询并高亮
    public Result searchTitleOrDescWithHits(String keyword) {
        return Result.ok(200,esActivityRepository.findTitleOrDesc(keyword));
    }

}
